package com.small.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.small.R;
import com.small.commom.Action;
import com.small.commom.AsyncImageLoader;
import com.small.commom.Common;
import com.small.model.DiscountListModel;
import com.small.model.DiscountModel;

public class DiscountAdapter extends BaseAdapter{

	List<DiscountModel> list; 
	LayoutInflater li;
	Context ctx;
	public DiscountAdapter(Context co,List<DiscountModel> _list){
		this.li=LayoutInflater.from(co);
		ctx = co;
		list=_list;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;//
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {  
		DiscountModel  item= list.get(position); 
		ViewHolder holder=null;  
			if(null==convertView){
				holder =new ViewHolder();  
				convertView=li.inflate(R.layout.activity_discounthome_list_item, null);
				holder.tv_title = (TextView) convertView.findViewById(R.id.item_name);     
		        holder.tv_content = (TextView) convertView.findViewById(R.id.item_content); 
		        holder. tv_disctance = (TextView) convertView.findViewById(R.id.tv_distance);
		        holder.img_title = (ImageView) convertView.findViewById(R.id.img_title); 
		        convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			} 
			 
			holder.tv_title.setText(item.getCouponTitle());
			holder.tv_content.setText(item.getCouponDesc());	  
			holder.tv_disctance.setText("距离您"+Action.GetShortDistance(Common.lon, Common.lat, item.getShopLongitude(), item.getShopLatitude())+"米");
			System.out.println(item.getCouponID());
			AsyncImageLoader.getInstance().loadImg(item.getCouponID(),item.getSmallImageUrl(),holder.img_title);
			 
		return convertView;  
	}
	
	private  class ViewHolder {    
    	public TextView tv_title;
    	public TextView tv_content;
    	public TextView tv_disctance;
    	public ImageView img_title; 
	} 
} 